package top.huntwolf.admin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.dao.AddressDao;
import top.huntwolf.admin.dao.ShopDao;
import top.huntwolf.admin.exception.ServiceException;
import top.huntwolf.admin.pojo.Address;
import top.huntwolf.admin.pojo.shop.BookShop;
import top.huntwolf.admin.utils.DistanceUtils;
import top.huntwolf.admin.utils.TokenUtil;

import java.util.List;

@Service
@Transactional
public class ShopService {

    @Autowired
    private ShopDao shopDao;

    public BookShop searchBookShopById(Integer id){
        return shopDao.selectById(id);
    }

    public List<BookShop> searchBookShop(){
        return shopDao.selectAll();
    }

    public BookShop searchRecentBookshop(double longitude,double latitude){
        //获取所有门店
        List<BookShop> bookShops = shopDao.selectAll();
        double distance = Double.MAX_VALUE;
        BookShop ans = new BookShop();
        //获取距离最短的门店
        for (BookShop bookShop : bookShops){
            //根据两点经纬度放回距离，单位为，米
            double d = DistanceUtils.getDistance(bookShop.getLongitude(),bookShop.getLatitude(),longitude,latitude);
            if(d<distance){
                distance = d;
                ans = bookShop;
            }
        }
        ans.setDistance(distance);
        return ans;
    }

}
